<%-- 
    Document   : CustomerForm
    Created on : 1 févr. 2010, 15:50:54
    Author     : JC
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="fr.uha.ensisa.kfetmanager.category.*
                                                            ,fr.uha.ensisa.kfetmanager.customer.*
                                                            , java.util.*"%>

<%
    boolean edit = request.getParameter("edit").equals("true");
    String firstName = "";
    String lastName = "";
    String nickname = "";
    boolean cotisant = false;
    boolean gerantKfet = false;
    long categoryId = 0;

    if (edit) {
        Customer c;
        try {
            c = (Customer) getServletContext().getAttribute("customer");
        } catch (Exception e) {
            getServletContext().setAttribute("errorMessage", "Objet Customer null");
            getServletContext().setAttribute("redirectTo", "/admin/customers");
            getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);
            return;
        }

        firstName = c.getFirstName();
        lastName = c.getLastName();
        nickname = c.getNickname();
        cotisant = c.isCotisant();
        gerantKfet = c.isGerantKfet();
        categoryId = c.getCategory().getId();
    }
%>

Pr&eacute;nom : <input type="text" name="firstName" value="<%= firstName %>"/><br/>
Nom de famille : <input type="text" name="lastName" value="<%= lastName %>"/><br/>
Alias :<input type="text" name="nickname" value="<%= nickname %>"/><br/>
Cotisant:
<input type="radio" name="cotisant" value="true" <%= cotisant ? "checked=\"checked\"" : "" %>/> Oui
<input type="radio" name="cotisant" value="false" <%= !cotisant ? "checked=\"checked\"" : "" %>/> Non<br/>
G&eacute;rant Kfet:
<input type="radio" name="gerantKfet" value="true" <%= gerantKfet ? "checked=\"checked\"" : "" %>/> Oui
<input type="radio" name="gerantKfet" value="false" <%= !gerantKfet ? "checked=\"checked\"" : "" %>/> Non<br/>
Cat&eacute;gorie : <select name="categoryId">
<%
    List<CustomerCategory> categoryList;
    try {
        categoryList = (List<CustomerCategory>) getServletContext().getAttribute("categoryList");
    } catch (Exception e) {
        categoryList = new LinkedList<CustomerCategory>();
    }

    if (categoryList.size() == 0) {
        getServletContext().setAttribute("errorMessage", "Aucune cat&eacute;gorie pr&eacute;sente. Merci de cr&eacute;er des cat&eacute;gories sur la page suivante.");
        getServletContext().setAttribute("redirectTo", "/admin/categories/create");
        getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);
        return;
    }

    Iterator<CustomerCategory> it = categoryList.iterator();
    long selectedIndex = 0;
    while(it.hasNext()) {
        CustomerCategory cat = it.next();
        out.write("<option value='");
        out.write(cat.getId().toString());
        out.write("' ");
        if ((!edit && selectedIndex == 0) || (edit && cat.getId() == categoryId)) out.write("selected='selected')");
        out.write(">");
        out.write(cat.getLabel());
        out.write("</option>\n");

        selectedIndex++;
    }
%>
</select><br/>

